package com.yin.lock8;

import java.util.concurrent.TimeUnit;

/**
 * 8lock
 * 一个对象只有一个锁，不同对象的锁不共用
 * 两个对象，两个同步方法，先执行call
 */
public class Test3 {

    public static void main(String[] args) {
        Phone3 phone1 = new Phone3();
        Phone3 phone2 = new Phone3();
        new Thread(phone1::sendMsg,"A").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(phone2::call,"B").start();
    }

}

class Phone3 {
    public synchronized void sendMsg() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("sendMsg");
    }

    public synchronized void call() {
        System.out.println("calling");
    }

    public void hello() {
        System.out.println("hello");
    }
}
